function changePaper(v){
	if("0"!==v){
		var arr=v.split("-");
		$("#totalScore").val(arr[1]);
		changeUser(arr[0]);
		
	}else{
		$("#totalScore").val("");
		$("#userScore").empty();
	}
}

$(function(){
    $('#save').on('click', function() {
    	var dataJson=getFormDatatoJson("paper");
    	var flag=validate(dataJson);
    	if(flag){
    		$.ajax({
            url: '/examReport/saveExaminationOnTheSpot',
            type: 'post',
            async:false,
            cache:false,
            data: dataJson,
		    success: function () {
		    	$.Zebra_Dialog('<div class="prompt"><div class="success-icon"></div><div class="success-title">保存成功!</div></div>', {
	                'title': '提示',
	                'buttons' : [ {
						caption : '确定',
						callback : function() {
							window.location.href="/examReport/examinationOnTheSpot";
						}
					} ]
	            });
		    	
		    }
    	});
    	}
    	
    });
    $('#update').on('click', function() {
    	var dataJson=getFormDatatoJson("paper");
    	var flag=validate(dataJson);
    	if(flag){
    		$.ajax({
        		url: '/examReport/updateExaminationOnTheSpot',
        		type: 'post',
        		async:false,
        		cache:false,
        		data: dataJson,
        		success: function () {
        			$.Zebra_Dialog('<div class="prompt"><div class="success-icon"></div><div class="success-title">修改成功!</div></div>', {
		                'title': '提示',
		                'buttons' : [ {
							caption : '确定',
							callback : function() {
								window.location.href="/examReport/examinationOnTheSpot";
							}
						} ]
		            });
        			
        		}
        	});
    	}
    	
    });
  
    
    
    
});
function validate(dataJson){
	 for (var key in dataJson)
	 {
		 if(key!=="paperId"){
			 if(!(/^\d+(\.\d+)?$/.test(dataJson[key]))&&!(/(^[0-9]\d*$)/.test(dataJson[key]))){
				 if(dataJson[key]===""||dataJson[key]===null){
					 
				 }else{
					 zebra_Dialog_warning("分值请输入非负数!");
					 return false;
				 }
	     			
	     	}
			var score=parseFloat(dataJson[key]);
     		var arr=(dataJson["paperId"]).split("-");
     		var total=parseFloat(arr[1]);
     		if(score>total){
     			zebra_Dialog_warning("输入分值不能大于试卷总分!");
     			return false;
     		}
		 }else if(key==="paperId"&&dataJson[key]==="0"){
			 zebra_Dialog_warning("请选择试卷!");
			 return false;
		 }
		 
	  }
	 return true;
}
function getFormDatatoJson(id) {
    var dataJson = {};
    $("#"+id).serializeArray().map(function (x) {
        if (dataJson[x.name] !== undefined) {
            if (!dataJson[x.name].push) {
                dataJson[x.name] = [dataJson[x.name]];
            }
            dataJson[x.name].push(x.value || '');
        } else  if(x.value){
            dataJson[x.name] = x.value || '';
        }
    });
    return dataJson;
}
function changeUser(paperId){
	$.ajax({
		url: '/examReport/findExaminationOnTheSpotUser',
		type: 'post',
		async:false,
		cache:false,
		data: {"paperId":paperId},
		success: function (data) {
			$("#userScore").empty();
			var str='';
			$.each(data,function(i,d){
				  str+='<div class="grade-list clearfix">'
					+'<div class="grade-listl">'+d[1]+'</div>'
					+'<div class="grade-listr"><input type="text" name="'+d[0]+'" class="list-input" /></div>'
					+'</div>';
			});
			
			$("#userScore").append(str);
		}
	});
}